##### 1. load packages
library(tidyverse)

##### 2. load dataset

signatories <- read.csv2(file="bandwagon.csv")
glimpse(signatories)

## X : Index number
## Handle :               Twitter username
## stay_on :              Users who signed both EU-wide coalition letters
## maintenance_100 :      Composite organisational maintenance indicator on a scale between 0 and 100
## stay_on_mod :          Users who continued to contest TTIP by joining the more radical STOP TTIP Alliance
## POS :                  Political opportunity structure - either open or closed
## POS_closed :           Same as above in a logical vector.  TRUE = closed, FALSE = open.
## mod_maintenance_100 :  Composite organisational maintenance indicator re-calculated within communities 


##### 3. H1: Does organisational maintenance predict ongoing contestation (signing lists 2 and 3)?
maintenance.glm <- glm(stay_on ~ maintenance_100, data = signatories, family = binomial)
# find signficance
summary(maintenance.glm)
### Z-score is 0.0539

### calculate log odds
exp(coefficients(maintenance.glm))
### log odds 1.0547

### calculate confidence intervals
confint(maintenance.glm)
### confidence intervals 1.012 - 1.130
###plot
#Data frame with organisational maintenance in ascending order
Predicted_data <- data.frame(maintenance_100=seq(
  min(signatories$maintenance_100), max(signatories$maintenance_100),len=67))
# Fill predicted values using regression model
Predicted_data$stay_on = predict(
  maintenance.glm, Predicted_data, type="response")

# Plot Predicted data and original data points
par(mar = c(6, 6, 6, 6))
plot(stay_on ~ maintenance_100, data=signatories, main="Organisational maintenance Hypothesis",
     xlab="Organisational maintenance",
     yaxt="n",
     ylab="Ongoing contestation")
lines(stay_on ~ maintenance_100, Predicted_data, lwd=2, col="red")
# axis labels
axis(2,                         
     at = 0:1,
     las= 2,
     labels = c("Dropped off", "Stayed on"))

##### 4. H2: Does radicalisation account for outlyers who drop off in spite of high organisational maintenance

### Use "stay_on_mod" considering that groups which dropped off EU_WIDE1 may continue to contest TTIP with a more radical campaign
# signatories of EU_WIDE1 who do not sign EU_WIDE2 but sign STOP_TTIP stay on 
# signatories of EU_WIDE1 who sign EU_WIDE2 but do not sign STOP TTIP drop off

###Logistic regression
faction.glm <- glm(stay_on_mod ~ maintenance_100, data = signatories, family = binomial)
##find signficance
summary(faction.glm)
### Z-score is 0.0116

###calculate log odds
exp(coefficients(faction.glm))
### log odds 1.389

###calculate confidence intervals
confint(faction.glm)
### confidence intervals 1.123 - 1.636
###plot
#Data frame with organisational maintenance in ascending order
Predicted_data <- data.frame(maintenance_100=seq(
  min(signatories$maintenance_100), max(signatories$maintenance_100),len=67))
# Fill predicted values using regression model
Predicted_data$stay_on_mod = predict(
  faction.glm, Predicted_data, type="response")

# Plot Predicted data and original data points
plot(stay_on_mod ~ maintenance_100, data=signatories, main="Factionalisation hypothesis",
     xlab="Organisational maintenance",
     yaxt="n",
     ylab="Ongoing contestation")
lines(stay_on_mod ~ maintenance_100, Predicted_data, lwd=2, col="red")

###axis labels?
axis(2,                         # Define x-axis manually
     at = 0:1,
     las= 2,
     labels = c("Moderate", "Radical"))

#####5. H3: Does within-community centrality (modified maintenance) predict political opportunity structure of those who stay on?

### Because periods of politicisation can be shorter within communities, I consider staying on as 
### either staying on the EU-wide coalition OR joining the more radical STOP TTIP alliance after signing the first EU-wide coalition letter
###   

group1 <- subset(signatories, stay_on == "TRUE")
group2 <- subset(signatories, stay_on_mod == "TRUE")

stay_on_all <- rbind(group1, group2)

### use mod_maintenance_100 - which is a score between 0-100 with centrality measures re-calculated within communities
###Logistic regression
domestication.glm <- glm(POS_closed ~ mod_maintenance_100, data = stay_on_all, family = binomial)
##find signficance
summary(domestication.glm)
### close again - Z-score is 0.010

###calculate log odds
exp(coefficients(domestication.glm))
### log odds 1.035

###calculate confidence intervals
confint(domestication.glm)
### confidence intervals 1.010 - 1.062

### Pearson coefficient
res <- cor.test(stay_on_all$mod_maintenance_100, as.integer(stay_on_all$POS_closed), 
                method = "pearson")
res
# moderate correlation: 0.356
# Association is highly significant (p<0.01)


###plot
#Data frame with organisational maintenance in ascending order
Predicted_data <- data.frame(mod_maintenance_100=seq(
  min(stay_on_all$mod_maintenance_100), max(stay_on_all$mod_maintenance_100),len=38))
# Fill predicted values using regression model
Predicted_data$POS_closed = predict(
  domestication.glm, Predicted_data, type="response")

# Plot Predicted data and original data points
plot(POS_closed ~ mod_maintenance_100, data=stay_on_all, main="Domestication hypothesis (stayed on only)",
     xlab="Organisational maintenance",
     yaxt="n",
     ylab="Political opportunity structure")
lines(POS_closed ~ mod_maintenance_100, Predicted_data, lwd=2, col="red")
###axis labels?
axis(2,                         # Define x-axis manually
     at = 0:1,
     las= 2,
     labels = c("Open", "Closed"))
